package barney.davis.hume.it3048todo.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import android.content.Context;
import barney.davis.hume.it3048todo.constants.TaskRepeat;
import barney.davis.hume.it3048todo.constants.TaskStatus;
import barney.davis.hume.it3048todo.dao.ITaskDAO;
import barney.davis.hume.it3048todo.dao.TaskDAO;
import barney.davis.hume.it3048todo.dto.Task;

public class TaskService implements ITaskService {

	ITaskDAO taskDAO;

	public TaskService(Context context) {
		taskDAO = new TaskDAO(context);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void saveTask(Task task) throws Exception {
		// We must also create a repeat of a task on save when the following
		// conditions are met:
		// 1. The task already exists in the database (i.e., we are performing
		// an update)
		// 2. The current task object has a repeat value other than none.

		// find out if this is an update or a new task
		
// Ran out of time to correctly implement the handling of repeats.
		
//		boolean taskExists;
//		Task existingTask = new Task();
//		try {
//			existingTask = taskDAO.fetch(task.getID());
//		} catch (Exception e) {
//			// do nothing. 
//			e.printStackTrace();
//		}
//		if (existingTask.getID().equals(task.getID())) {
//			taskExists = true;
//		} else {
//			taskExists = false;
//		}


		
		// if task already exists, make a new one if repeat is other than none.
//		TaskRepeat desiredRepeat = task.getRepeat();
//		if (taskExists && (desiredRepeat != TaskRepeat.NONE)) {
//			Task newTask = new Task();
//			newTask.setComplexity(task.getComplexity());
//			newTask.setLocation(task.getLocation());
//			newTask.setLocationLabel(task.getLocationLabel());
//			newTask.setRepeat(task.getRepeat());
//			newTask.setStatus(TaskStatus.INPROGRESS);
//			newTask.setText(task.getText());
//			Calendar newDate = new GregorianCalendar();
//			newDate = task.getDueDate();
//			if (desiredRepeat == TaskRepeat.DAILY) {
//				newDate.roll(Calendar.DATE, 1);
//			} else if (desiredRepeat == TaskRepeat.MONTHY) {
//				newDate.roll(Calendar.MONTH, 1);
//			} else if (desiredRepeat == TaskRepeat.WEEKDAYS) {
//				// make sure we're not creating a new due date that falls on a
//				// weekend
//				int dayOfWeek = newDate.get(Calendar.DAY_OF_WEEK);
//				if (dayOfWeek == Calendar.FRIDAY) {
//					newDate.roll(Calendar.MONTH, 3);
//				} else if (dayOfWeek == Calendar.SATURDAY) {
//					newDate.roll(Calendar.MONTH, 2);
//				} else {
//					newDate.roll(Calendar.MONTH, 1);
//				}
//			} else if (desiredRepeat == TaskRepeat.WEEKENDS) {
//				int dayOfWeek = newDate.get(Calendar.DAY_OF_WEEK);
//				if (dayOfWeek == Calendar.SATURDAY) {
//					newDate.roll(Calendar.DATE, 1);
//				} else {
//					newDate.roll(Calendar.DATE, (7 - dayOfWeek));
//				}
//			}
//
//			newTask.setDueDate(newDate);
//			taskDAO.save(newTask);
//		}

		taskDAO.save(task);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ArrayList<Task> getTasks() throws Exception {
		ArrayList<Task> rtrn = new ArrayList<Task>();
		rtrn = taskDAO.fetchAll();
		return rtrn;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ArrayList<Task> getTasks(Calendar dueDate) throws Exception {
		ArrayList<Task> rtrn = new ArrayList<Task>();
		rtrn = taskDAO.fetchByDueDate(dueDate, dueDate);
		return rtrn;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ArrayList<Task> getTasks(Calendar startDate, Calendar endDate) throws Exception {
		ArrayList<Task> rtrn = new ArrayList<Task>();
		rtrn = taskDAO.fetchByDueDate(startDate, endDate);
		return rtrn;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public ArrayList<Task> getTasks(TaskStatus status) throws Exception {
		ArrayList<Task> rtrn = new ArrayList<Task>();
		rtrn = taskDAO.fetchByStatus(status);
		return rtrn;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void deleteTask(Task task) throws Exception {
		taskDAO.delete(task);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Task getTaskById(String id) throws Exception {
		return taskDAO.fetch(id);
	}

}
